package org.example.sys.service;

import org.example.sys.bean.TableData;
import org.example.sys.dao.BookDao;
import org.example.sys.model.Book;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Sort;

import javax.annotation.Resource;
import java.util.Optional;

@Service
public class BookService extends PageService<Book> {

    @Resource
    BookDao bookDao;

    public boolean save(Book book) {
        bookDao.save(book);
        return true;
    }

    public boolean deleteById(int id) {
        bookDao.deleteById(id);
        return bookDao.existsById(id);
    }

    public boolean update(Book book) {
        Optional<Book> bookOptional = bookDao.findById(book.getId());
        if (bookOptional.isPresent()) {
            bookDao.save(book);
            return true;
        }
        return false;
    }

    public Book query(int id) {
        Optional<Book> bookOptional = bookDao.findById(id);
        return bookOptional.orElse(null);
    }

    public TableData<Book> tableData(Integer pageNo, Integer pageSize) {
        return tableData(bookDao, pageNo, pageSize);
    }

    public TableData<Book> tableData(Integer pageNo, Integer pageSize, Sort sort) {
        return tableData(bookDao, pageNo, pageSize, sort);
    }
}
